<?php
/**
 * 会员中心
 * @author lizhi7  <lizhi7@xin.com>
 * @time: 2018-02-22 16:23
 */

namespace app\controllers;
use app\models\LoginForm;
use app\models\SignupForm;
use app\models\User;
use app\models\UserExt;
use yii\web\Controller;
use Yii;

class UserController extends Controller
{

    /**
     * 会员中心
     * @return string|\yii\web\Response
     */
    public function actionIndex()
    {
        $userId = Yii::$app->user->getId();
        $user = new User();
        $user->setUserHash();
        $userExt = new UserExt();
        $userExtRow = $userExt->find()
            ->where('user_id=:user_id', [':user_id' => $userId])->one();
        return $this->render('index',['userExtRow' => $userExtRow]);
    }

    /**
     * 注册
     * Displays homepage.
     * @return string
     */
    public function actionRegister()
    {
        $mobile = Yii::$app->request->post('mobile');
        $password = Yii::$app->request->post('password');
        $repassword = Yii::$app->request->post('repassword');
        $user = new User();
        $userRow = $user->find()->where(['mobile' => $mobile])->one();
        if(!empty($userRow)) {
            return $this->asJson(['code' => 10001, 'msg' => '手机号码已注册']);
        }
        if ($repassword != $password) {
            return $this->asJson(['code' => 10002, 'msg' => '两次输入密码不一致']);
        }
        $postData = Yii::$app->request->post();
        $signUp = new SignupForm();
        if ($signUp->load(['SignupForm' => $postData]) && $signUp->signup()) {
            $userId = Yii::$app->db->getLastInsertId();
            $userExt = new UserExt();
            $userExt->user_id = $userId;
            $userExt->limit_prize = 0;
            $userExt->updated_at = $userExt->created_at = time();
            $userExt->invite_code = $this->getrandstr();
            $userExt->save();
            return $this->asJson(['code' => 10000, 'msg' => '恭喜注册成功']);
        }
    }

    /**
     * 登录信息
     * @return \yii\web\Response
     */
    public function actionLogin()
    {
        $model = new LoginForm();
        $postRow = ['LoginForm' => Yii::$app->request->post()];
        if ($model->load($postRow) && $model->login()) {
            $url = '/user/index';
            if (Yii::$app->user->identity->status == 0) {
                $url = '/user/active';
            }
            return $this->asJson(['code' => 10000, 'msg' => '登录成功', 'url' => $url]);
        }
        return $this->asJson(['code' => 10001, 'msg' => '登录失败']);
    }

    /**
     * 激活首页
     * @return string|\yii\web\Response
     */
    public function actionActive()
    {
        if (is_null(Yii::$app->user->identity)) {
            return $this->goHome();
        }
        $userId = Yii::$app->user->getId();
        $User = new User();
        $userRow = $User->findIdentity($userId);
        return $this->render('active', ['userRow' => $userRow]);
    }

    /**
     * 绑定钱包
     * @return \yii\web\Response
     */
    public function actionBuildWallet()
    {
        $User = new User();
        $wallet_addr = Yii::$app->request->post('wallet_addr');
        if (empty($wallet_addr)) {
            return $this->asJson(['code' => 10001, 'msg' => '钱包地址非法']);
        }
        $userRow = $User->findIdentity(Yii::$app->user->identity->user_id);
        $userRow->wallet_addr = $wallet_addr;
        //$userRow->validate();$errors = $userRow->getErrors();
        $userRow->save();
        return $this->asJson(['code' => 10000, 'msg' => '绑定钱包成功']);
    }

    /**
     * 检测激活
     * @return \yii\web\Response
     */
    public function actionCheckActive()
    {
        $User = new User();
        $userRow = $User->findIdentity(Yii::$app->user->identity->user_id);
        if ($userRow->balance <= 0) {
            return $this->asJson(['code' => 10001, 'msg' => '充值需要一段时间的等候']);
        }
        return $this->asJson(['code' => 10000, 'msg' => '您已经完成激活']);
    }

    /**
     * 登录检测
     * @return \yii\web\Response
     */
    public function actionLoginMsg()
    {
        $mobile = Yii::$app->request->post('mobile');
        $user = new User();
        $userRow = $user->find()->where(['mobile' => $mobile])->one();
        if(empty($userRow)){
            return $this->asJson(['code' => 10001, 'msg' => '该手机号未注册用户']);
        }
    }
    public function getrandstr(){
        $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分
        return $rands;
    }
}